package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.TableData;
import com.ruoyi.system.domain.TemplateTableRel;
import com.ruoyi.system.domain.vo.TemplateTableRelVo;
import com.ruoyi.system.service.ITableDataService;
import com.ruoyi.system.service.ITemplateTableRelService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TemplateData;
import com.ruoyi.system.service.ITemplateDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 业务模板Controller
 *
 * @author chenming
 * @date 2024-10-16
 */
@RestController
@RequestMapping("/system/templateData")
public class TemplateDataController extends BaseController
{
    @Autowired
    private ITemplateDataService templateDataService;

    @Autowired
    private ITemplateTableRelService templateTableRelService;

    @Autowired
    private ITableDataService tableDataService;

    /**
     * 查询业务模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:templateData:list')")
    @GetMapping("/list")
    public TableDataInfo list(TemplateData templateData)
    {
        startPage();
        List<TemplateData> list = templateDataService.selectTemplateDataList(templateData);
        list.forEach(
                template -> {
                    if(null != template.getTableId()){
                        TableData tableData = tableDataService.selectTableDataByTableId(template.getTableId());
                        template.setTableName(tableData.getTableCnName()); // 显示中文名称
                    }
                }
        );
        return getDataTable(list);
    }

    /**
     * 导出业务模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:templateData:export')")
    @Log(title = "业务模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TemplateData templateData)
    {
        List<TemplateData> list = templateDataService.selectTemplateDataList(templateData);
        ExcelUtil<TemplateData> util = new ExcelUtil<TemplateData>(TemplateData.class);
        util.exportExcel(response, list, "业务模板数据");
    }

    /**
     * 获取业务模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:templateData:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId)
    {
        return success(templateDataService.selectTemplateDataByTemplateId(templateId));
    }

    /**
     * 新增业务模板
     */
    @PreAuthorize("@ss.hasPermi('system:templateData:add')")
    @Log(title = "业务模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TemplateData templateData)
    {
        return toAjax(templateDataService.insertTemplateData(templateData));
    }

    /**
     * 修改业务模板
     */
    @PreAuthorize("@ss.hasPermi('system:templateData:edit')")
    @Log(title = "业务模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TemplateData templateData)
    {
        return toAjax(templateDataService.updateTemplateData(templateData));
    }

    /**
     * 删除业务模板
     */
    @PreAuthorize("@ss.hasPermi('system:templateData:remove')")
    @Log(title = "业务模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable Long[] templateIds)
    {
        return toAjax(templateDataService.deleteTemplateDataByTemplateIds(templateIds));
    }
}
